<template>
  <div>
    <el-card class="box-card" style="margin: auto">
      <el-row>
        <el-col :span="10" style="margin-top: 5px"
          >Total {{ totalfile }} files</el-col
        >
        <el-col :span="8">
          <div class="demo-input-suffix">
            Search
            <el-input
              placeholder="文件名"
              prefix-icon="el-icon-search"
              v-model="keyword"
              style="width: 200px"
              @keypress.enter.native="search"
            ></el-input>
          </div>
        </el-col>
      </el-row>
      <el-table :data="fileList" style="width: 100%" border stripe>
        <el-table-column prop="dname" label="文件名"></el-table-column>
        <el-table-column
          prop="dsize"
          label="大小"
          width="280"
        ></el-table-column>
        <el-table-column
          prop="ctime"
          label="创建日期"
        ></el-table-column>
        <el-table-column fixed="right" label="操作" width="80">
          <template slot-scope="scope">
            <a
              class="el-icon-download"
              v-bind:href="fileList[scope.$index].path"
            ></a>
            <i class="el-icon-delete" @click="delFile(scope.$index)"></i>
          </template>
        </el-table-column>
      </el-table>
      <el-row style="margin-top: 25px">
        <el-col>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 20, 40]"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalfile"
          ></el-pagination>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import filesApi from "@/api/ky";

export default {
  data() {
    return {
      keyword: "",
      currentPage: 1, //初始页
      pagesize: 10,
      fileList: [],
      paths: [],
      totalfile: 0,
    };
  },
  methods: {
    search() {
      if (this.keyword.trim == "") {
        return this.fileList;
      }
      return this.getFileList();
    },
    delFile(index) {
      //this.fileList.splice(index, 1);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          filesApi.delFile(this.fileList[index].id).then((res) => {
            console.log(res);
            if (res.data.flag == true) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
            this.getFileList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange(size) {
      this.pagesize = size;
      this.getFileList();
      //  console.log(this.pagesize)  //每页下拉显示数据
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.getFileList();
      // console.log(this.currentPage)  //点击第几页
    },

    //获取文件列表
    getFileList() {
      var Id = this.$store.state.id;
      // console.log(Id)
      if (this.$route.path == "/admin") {
        filesApi
          .getFiles(this.pagesize, this.currentPage, this.keyword)
          .then((res) => {
            this.fileList = res.data.rows;
            this.totalfile = res.data.total;
          });
      } else {
        this.$http.get("user/myfilelist?id=" + Id).then((res) => {
           this.fileList = res.body.data.rows;
          this.totalfile = res.body.data.total;
           
        });
      }
    },
  },
  beforeMount() {
    this.getFileList();
  },
};
</script>

<style>
.el-row {
  margin-bottom: 15px;
}
</style>